import {Util} from "@lowcode/jpaas-common-lib";
import FormCodegenVariableApi from "@/api/form/codegen/formCodegenVariable";

export default {
    props: ['formData'],
    data() {
        return {
            settingJson:{},
            panelActiveKeys:["globalVars"],
            labelCol: { span:6 },
            wrapperCol: { span:18 },
            labelCol1: { span:3 },
            wrapperCol1: { span:19 },
            vars:[],
            //全局变量
            globalVars:{},
            //自定义变量
            customVars:[],
            varsColumns:[
                {title: this.i18n('varsColumns.name','变量名'), dataIndex: 'name', width: 130, scopedSlots: {customRender: 'name'}},
                {title: this.i18n('varsColumns.value','变量值'), dataIndex: 'value', width: 130, scopedSlots: {customRender: 'value'}},
                {title: this.i18n('varsColumns.desc','变量备注'), dataIndex: 'desc', width: 130, scopedSlots: {customRender: 'desc'}}
            ]
        }
    },
    computed: {
        arrLen() {
            let len = Math.ceil(this.vars.length / 2);
            if (len == 0) {
                len = len + 1
            }
            return len
        }
    },
    created() {
        this.init()
    },
    methods: {
        i18n(name,text,key){
            return this.$ti18(name,text,"formCodegenProject.edit2",key);
        },
        init() {
            this.settingJson = JSON.parse(this.formData.settingJson);
            if(this.settingJson.hasOwnProperty("customVars") && this.settingJson.customVars.length>0){
                this.customVars=Util.deepClone(this.settingJson.customVars);
            }
            var self=this;
            this.initGlobalVars(function(vars){
                self.vars = vars;
                var globalVars={};
                if(self.settingJson.hasOwnProperty("globalVars")){
                    globalVars=Util.deepClone(self.settingJson.globalVars);
                }
                for(var i=0;i<vars.length;i++){
                    var obj=vars[i];
                    self.$set(self.globalVars,obj.name,globalVars[obj.name] || obj.value);
                }
            });
        },
        addCustom(){
            this.customVars.push({name:'',value:'',desc:''})
        },
        removeCustom(){
            this.$refs.customVar.removeSelected();
        },
        getData(){
            //自定义变量排除空数据
            let customVars=this.customVars.filter(item=>{return item.name && item.value});
            let data={globalVars:this.globalVars,customVars:customVars};
            let result={success:true,data:data};
            for(let i=0;i<customVars.length;i++){
                if(this.globalVars.hasOwnProperty(customVars[i].name)){
                    result.success=false;
                    result.message=this.i18n('customVar','自定义变量')+"("+customVars[i].name+")"+this.i18n('hasInGlobVar','在全局变量中已存在！');
                    return result;
                }
            }
            return result;
        },
        initGlobalVars(callback){
            FormCodegenVariableApi.getAllVars().then(res=>{
                if(res.success) {
                    callback(res.data);
                }
            })
        }
    }
}
